<html>
<head><meta charset="utf-8"><title>Array of pointers to methods, for type with lifetimes? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html">Array of pointers to methods, for type with lifetimes?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="193620518"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193620518" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Arlie Davis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193620518">(Apr 10 2020 at 21:36)</a>:</h4>
<p>I'm working on an interpreter for a niche language. It uses a dispatch table to point to functions; pretty simple. However, I've tried converting these functions to methods, and I can't seem to find any way to express the <code>fn</code> type that goes in the dispatch table.</p>
<p>My instruction handler functions all take a <code>&amp;mut Foo&lt;'a&gt;</code>, where Foo is my interpreted program context and 'a is a lifetime parameter for data that the interpreted program operates on.  If I write the handlers as free functions <code>fn do_thing&lt;'a&gt;(&amp;mut Foo&lt;'a&gt;) { ... }</code>, then I can build a dispatch table: <code>static DISPATCH_TABLE: [fn(&amp;mut Foo&lt;'_&gt;)] = ...</code>.  But if I convert a function to an associated function / method, then the <code>Foo::do_thing</code> items cannot be converted to the type that I'm using for the dispatch table.</p>
<p>Is there a way to express this?  Or is this not possible?</p>
<p>Playground link: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=c6180a4457c595cec9f1768bb49c4003" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=c6180a4457c595cec9f1768bb49c4003">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=c6180a4457c595cec9f1768bb49c4003</a></p>



<a name="193645965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193645965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193645965">(Apr 11 2020 at 06:50)</a>:</h4>
<p><span class="user-mention" data-user-id="274471">@Arlie Davis</span> hm, that is interesting. I would have expected this to work. I think the fact that you have two lifetime parameters in these functions is also involved in causing the problem here.</p>



<a name="193645972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193645972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193645972">(Apr 11 2020 at 06:51)</a>:</h4>
<p>There might be already a bug report for this somewhere (<span class="user-mention" data-user-id="119009">@eddyb</span> ?), but I'd say it is also justified to open a new report.</p>



<a name="193652172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193652172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193652172">(Apr 11 2020 at 09:51)</a>:</h4>
<p>If you wrap in a closure, it works though: |t| Foo::do_fancy_thing(t),  // &lt;-- now it succeeds</p>



<a name="193652454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193652454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193652454">(Apr 11 2020 at 09:59)</a>:</h4>
<p><span class="user-mention" data-user-id="274471">@Arlie Davis</span> in <code>impl&lt;'a&gt; Foo&lt;'a&gt;</code>, <code>'a</code> is early-bound which means you can't turn it into the late-bound version</p>



<a name="193652519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193652519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193652519">(Apr 11 2020 at 10:00)</a>:</h4>
<p>this is nothing you should be able to observe <em>sigh</em> I wonder if Chalk has a better model</p>



<a name="193652553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193652553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193652553">(Apr 11 2020 at 10:01)</a>:</h4>
<p>I feel like maybe <code>ty::FnDef</code> should have a <code>Binder</code> or something so it can late-bind early-bound lifetimes (cc <span class="user-mention" data-user-id="116009">@nikomatsakis</span>)</p>



<a name="193652607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193652607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193652607">(Apr 11 2020 at 10:02)</a>:</h4>
<p>I think <span class="user-mention" data-user-id="119009">@eddyb</span>  is saying that opening an issue for this would be worth it. ;)</p>



<a name="193652611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193652611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193652611">(Apr 11 2020 at 10:02)</a>:</h4>
<p>no</p>



<a name="193652615"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193652615" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193652615">(Apr 11 2020 at 10:02)</a>:</h4>
<p>there's probably a dozen issues about this :P</p>



<a name="193652618"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193652618" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193652618">(Apr 11 2020 at 10:02)</a>:</h4>
<p>that nobody has triaged in ages or w/e</p>



<a name="193652623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193652623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193652623">(Apr 11 2020 at 10:03)</a>:</h4>
<p>I don't know the landscape well, just the root cause</p>



<a name="193652662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193652662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193652662">(Apr 11 2020 at 10:04)</a>:</h4>
<p>since it's a ZST struct, it can be as generic as you need it to be - e.g. it could be like:</p>
<div class="codehilite"><pre><span></span><span class="k">struct</span> <span class="nc">Foo_do_fancy_thing</span><span class="p">;</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Fn</span><span class="p">(</span><span class="o">&amp;</span><span class="na">&#39;s</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">Foo</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Foo_do_fancy_thing</span><span class="w"> </span><span class="p">{...}</span><span class="w"></span>
</pre></div>



<a name="193652678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193652678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193652678">(Apr 11 2020 at 10:04)</a>:</h4>
<p>which is pretty much what you get from introducing a closure :P</p>



<a name="193652686"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193652686" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193652686">(Apr 11 2020 at 10:04)</a>:</h4>
<p><span class="user-mention" data-user-id="274471">@Arlie Davis</span> I think <span class="user-mention" data-user-id="281572">@marmeladema</span>'s suggestion will work well enough if you wrap it in a macro or something</p>



<a name="193678756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193678756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Arlie Davis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193678756">(Apr 11 2020 at 20:46)</a>:</h4>
<p>Ah, thank you all. I don't understand early-bound vs. late-bound, so I'm going to go learn about that. Meanwhile, @eddyb, it sounds like you think it's not worth opening an issue on this?  Why not? It seems like a reasonable thing to do, and the asymmetry between pointers-to-fns and pointers-to-methods seems undesirable.</p>



<a name="193678839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193678839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193678839">(Apr 11 2020 at 20:49)</a>:</h4>
<p>I think what eddyb meant is that there are most certainly already issues for that.</p>



<a name="193678847"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193678847" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Arlie Davis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193678847">(Apr 11 2020 at 20:49)</a>:</h4>
<p>Gotcha. Thanks.</p>



<a name="193686685"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Array%20of%20pointers%20to%20methods%2C%20for%20type%20with%20lifetimes%3F/near/193686685" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Array.20of.20pointers.20to.20methods.2C.20for.20type.20with.20lifetimes.3F.html#193686685">(Apr 12 2020 at 00:30)</a>:</h4>
<p><span class="user-mention" data-user-id="274471">@Arlie Davis</span> you can try to find an existing issue and add a comment to it, maybe</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>